package com.puzzlebazar.client.squaregrid.control;

import com.google.gwt.dom.client.NativeEvent;
import com.google.gwt.event.dom.client.MouseEvent;
import com.google.gwt.event.shared.EventHandler;
import com.puzzlebazar.client.puzzle.model.History;
import com.puzzlebazar.client.puzzle.model.MoveTimer;
import com.puzzlebazar.client.squaregrid.model.StateModel;
import com.puzzlebazar.client.util.Vec2i;

public class CellStateDirectControl extends CellStateControl {

	/**
	 * Implements a "direct-control" feel, where each button is directly associated
	 * with a state. This works for 3 states or less (Left button = state 1, 
	 * right button = state 2. Double click to erase.)
	 * 
	 * @param stateModel The model to control 
	 * @param history The history track to control (null if no history is desired)
	 * @param moveTimer The timer model to extract the time at which the move have been performed
	 */
	public CellStateDirectControl( StateModel stateModel, History history, MoveTimer moveTimer ) {
		super(stateModel, history, moveTimer);
		assert stateModel.getNbStates() <= 3;		
	}
	

	@Override
	public int stateToGenerate( Vec2i loc, MouseEvent<? extends EventHandler> event  )  throws Exception {
		
		int newState = 1;
		if( getNbStates() == 3 &&			
				(event.getNativeButton() == NativeEvent.BUTTON_RIGHT || event.isShiftKeyDown()) ) {
			newState = 2;
		}
		
		if( getState(loc) == newState ) 
			newState = 0;
		
		return newState;
	}

	@Override
	public void release() {
		super.release();
	}
	
}